/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.lifesting.wfs.runtime.impl;

import com.lifesting.wfs.model.Node;

import com.lifesting.wfs.runtime.ProcessInstance;
import com.lifesting.wfs.runtime.RuntimePackage;
import com.lifesting.wfs.runtime.Token;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Token</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.lifesting.wfs.runtime.impl.TokenImpl#getChildren <em>Children</em>}</li>
 *   <li>{@link com.lifesting.wfs.runtime.impl.TokenImpl#getParent <em>Parent</em>}</li>
 *   <li>{@link com.lifesting.wfs.runtime.impl.TokenImpl#getPlace <em>Place</em>}</li>
 *   <li>{@link com.lifesting.wfs.runtime.impl.TokenImpl#getProcessInstance <em>Process Instance</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class TokenImpl extends EObjectImpl implements Token {
	/**
	 * The cached value of the '{@link #getChildren() <em>Children</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getChildren()
	 * @generated
	 * @ordered
	 */
	protected EList<Token> children;

	/**
	 * The cached value of the '{@link #getPlace() <em>Place</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPlace()
	 * @generated
	 * @ordered
	 */
	protected Node place;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TokenImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return RuntimePackage.Literals.TOKEN;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Token> getChildren() {
		if (children == null) {
			children = new EObjectContainmentWithInverseEList<Token>(Token.class, this, RuntimePackage.TOKEN__CHILDREN, RuntimePackage.TOKEN__PARENT);
		}
		return children;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Token getParent() {
		if (eContainerFeatureID != RuntimePackage.TOKEN__PARENT) return null;
		return (Token)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetParent(Token newParent, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newParent, RuntimePackage.TOKEN__PARENT, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setParent(Token newParent) {
		if (newParent != eInternalContainer() || (eContainerFeatureID != RuntimePackage.TOKEN__PARENT && newParent != null)) {
			if (EcoreUtil.isAncestor(this, newParent))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newParent != null)
				msgs = ((InternalEObject)newParent).eInverseAdd(this, RuntimePackage.TOKEN__CHILDREN, Token.class, msgs);
			msgs = basicSetParent(newParent, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, RuntimePackage.TOKEN__PARENT, newParent, newParent));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Node getPlace() {
		if (place != null && place.eIsProxy()) {
			InternalEObject oldPlace = (InternalEObject)place;
			place = (Node)eResolveProxy(oldPlace);
			if (place != oldPlace) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, RuntimePackage.TOKEN__PLACE, oldPlace, place));
			}
		}
		return place;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Node basicGetPlace() {
		return place;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPlace(Node newPlace) {
		Node oldPlace = place;
		place = newPlace;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, RuntimePackage.TOKEN__PLACE, oldPlace, place));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ProcessInstance getProcessInstance() {
		if (eContainerFeatureID != RuntimePackage.TOKEN__PROCESS_INSTANCE) return null;
		return (ProcessInstance)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetProcessInstance(ProcessInstance newProcessInstance, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newProcessInstance, RuntimePackage.TOKEN__PROCESS_INSTANCE, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setProcessInstance(ProcessInstance newProcessInstance) {
		if (newProcessInstance != eInternalContainer() || (eContainerFeatureID != RuntimePackage.TOKEN__PROCESS_INSTANCE && newProcessInstance != null)) {
			if (EcoreUtil.isAncestor(this, newProcessInstance))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newProcessInstance != null)
				msgs = ((InternalEObject)newProcessInstance).eInverseAdd(this, RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN, ProcessInstance.class, msgs);
			msgs = basicSetProcessInstance(newProcessInstance, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, RuntimePackage.TOKEN__PROCESS_INSTANCE, newProcessInstance, newProcessInstance));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void signal(String to) {
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		throw new UnsupportedOperationException();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case RuntimePackage.TOKEN__CHILDREN:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getChildren()).basicAdd(otherEnd, msgs);
			case RuntimePackage.TOKEN__PARENT:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetParent((Token)otherEnd, msgs);
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetProcessInstance((ProcessInstance)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case RuntimePackage.TOKEN__CHILDREN:
				return ((InternalEList<?>)getChildren()).basicRemove(otherEnd, msgs);
			case RuntimePackage.TOKEN__PARENT:
				return basicSetParent(null, msgs);
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				return basicSetProcessInstance(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID) {
			case RuntimePackage.TOKEN__PARENT:
				return eInternalContainer().eInverseRemove(this, RuntimePackage.TOKEN__CHILDREN, Token.class, msgs);
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				return eInternalContainer().eInverseRemove(this, RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN, ProcessInstance.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case RuntimePackage.TOKEN__CHILDREN:
				return getChildren();
			case RuntimePackage.TOKEN__PARENT:
				return getParent();
			case RuntimePackage.TOKEN__PLACE:
				if (resolve) return getPlace();
				return basicGetPlace();
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				return getProcessInstance();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case RuntimePackage.TOKEN__CHILDREN:
				getChildren().clear();
				getChildren().addAll((Collection<? extends Token>)newValue);
				return;
			case RuntimePackage.TOKEN__PARENT:
				setParent((Token)newValue);
				return;
			case RuntimePackage.TOKEN__PLACE:
				setPlace((Node)newValue);
				return;
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				setProcessInstance((ProcessInstance)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case RuntimePackage.TOKEN__CHILDREN:
				getChildren().clear();
				return;
			case RuntimePackage.TOKEN__PARENT:
				setParent((Token)null);
				return;
			case RuntimePackage.TOKEN__PLACE:
				setPlace((Node)null);
				return;
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				setProcessInstance((ProcessInstance)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case RuntimePackage.TOKEN__CHILDREN:
				return children != null && !children.isEmpty();
			case RuntimePackage.TOKEN__PARENT:
				return getParent() != null;
			case RuntimePackage.TOKEN__PLACE:
				return place != null;
			case RuntimePackage.TOKEN__PROCESS_INSTANCE:
				return getProcessInstance() != null;
		}
		return super.eIsSet(featureID);
	}

} //TokenImpl
